//
//  DHSale_GoodShopView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/11.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSale_GoodShopView.h"
#import "DHSale_GoodShopCell.h"

@interface DHSale_GoodShopView()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataArr;
@property (assign, nonatomic) NSInteger pageIndex;

@end

@implementation DHSale_GoodShopView

+ (DHSale_GoodShopView *)shareDHSale_GoodShopView{
    DHSale_GoodShopView *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DHSale_GoodShopView class]) owner:self options:nil] objectAtIndex:0];
    return view;
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)loadGoodShopViewData{
    [self loadProduct];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHSale_GoodShopCell" bundle:nil] forCellReuseIdentifier:kDHSale_GoodShopCell];
    self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadProduct)];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreProduct)];
}

- (void)loadProduct{
    self.pageIndex = 0;
    [self.tableView.mj_footer resetNoMoreData];
    NSDictionary *filter_Dic = [[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@(self.pageIndex) forKey:@"pageIndex"];
    [params setObject:@(20) forKey:@"pageSize"];
    if ([[filter_Dic objectForKey:@"color"] length] > 0) {
        [params setObject:[filter_Dic objectForKey:@"color"] forKey:@"color"];
    }
    if ([[filter_Dic objectForKey:@"label_id"] length] > 0) {
        [params setObject:[filter_Dic objectForKey:@"label_id"] forKey:@"label_id"];
    }
    if ([[filter_Dic objectForKey:@"style_id"] length] > 0) {
        [params setObject:[filter_Dic objectForKey:@"style_id"] forKey:@"style_id"];
    }
    if ([[filter_Dic objectForKey:@"sex"] length] > 0) {
        [params setObject:[filter_Dic objectForKey:@"sex"] forKey:@"sex"];
    }
    if ([[filter_Dic objectForKey:@"category_id"] length] > 0) {
        [params setObject:[filter_Dic objectForKey:@"category_id"] forKey:@"category_id"];
    }
    [[DHNetAPIClient shareDHNetAPIClient] request_products_goodshopsWithParams:params andBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.dataArr removeAllObjects];
        [self.dataArr addObjectsFromArray:[data objectForKey:@"goodshops"]];
        if (self.dataArr.count < 20) {
            self.tableView.mj_footer.hidden = YES;
        }
        self.pageIndex += 1;
        [self.tableView reloadData];
    }];
}

- (void)loadMoreProduct{
    NSDictionary *filter_Dic = [[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@(self.pageIndex) forKey:@"pageIndex"];
    [params setObject:@(20) forKey:@"pageSize"];
    if ([[filter_Dic objectForKey:@"color"] length] > 0) {
        [params setObject:[filter_Dic objectForKey:@"color"] forKey:@"color"];
    }
    if ([[filter_Dic objectForKey:@"label_id"] length] > 0) {
        [params setObject:[filter_Dic objectForKey:@"label_id"] forKey:@"label_id"];
    }
    if ([[filter_Dic objectForKey:@"style_id"] length] > 0) {
        [params setObject:[filter_Dic objectForKey:@"style_id"] forKey:@"style_id"];
    }
    if ([[filter_Dic objectForKey:@"sex"] length] > 0) {
        [params setObject:[filter_Dic objectForKey:@"sex"] forKey:@"sex"];
    }
    if ([[filter_Dic objectForKey:@"category_id"] length] > 0) {
        [params setObject:[filter_Dic objectForKey:@"category_id"] forKey:@"category_id"];
    }
    [[DHNetAPIClient shareDHNetAPIClient] request_products_goodshopsWithParams:params andBlock:^(id data, NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [self.dataArr addObjectsFromArray:[data objectForKey:@"goodshops"]];
        if (self.dataArr.count < 20 * (self.pageIndex + 1)) {
            self.tableView.mj_footer.hidden = YES;
        }
        self.pageIndex += 1;
        [self.tableView reloadData];
    }];
}

#pragma mark - UITableViewDelegate and UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.dataArr[indexPath.row];
    NSArray *arr = [dic objectForKey:@"products"];
    CGFloat w = (kScreenWidth - 32) / 3;
    CGFloat h = w * 1.29 + 76;

    if (arr.count % 3 == 0) {
        return 83.5 + (arr.count / 3) * h;
    }else{
        return 83.5 + (arr.count / 3 + 1) * h;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHSale_GoodShopCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHSale_GoodShopCell forIndexPath:indexPath];
    [cell updateCell:self.dataArr[indexPath.row]];
    return cell;
}

@end
